<template>
  <div>
    <div class="post-img" v-if="postDetail.type&&postDetail.type===1">
      <!-- 头部部分 -->
      <div class="header">
        <div class="header-left">
          <span class="iconfont iconjiantou2" @click="goback"></span>
          <div class="logo">
            <span class="iconfont iconnew"></span>
          </div>
        </div>
        <div
          class="header-Btn"
          @click="handleFollow"
          :class="{hasFollow:postDetail.has_follow}"
        >{{postDetail.has_follow?'已关注':'关注'}}</div>
      </div>
      <!-- 标题部分 -->
      <div class="titlePost">
        <p class="title">{{postDetail.title}}</p>
        <div class="info">
          <span>{{postDetail.user.nickname}}</span>
          <span>{{postDetail.create_date.split("T")[0]}}</span>
        </div>
      </div>
      <!-- 内容部分 -->
      <div class="mainContent" v-html="postDetail.content"></div>
    </div>
    <!-- 这里是视频文章 -->
    <div class="post-video" v-else-if="postDetail.type&&postDetail.type===2">
      <div class="mainVideo">
        <video
          src="https://video.pearvideo.com/mp4/third/20200219/cont-1653429-10887340-001410-hd.mp4"
          controls
          poster="https://timgmb04.bdimg.com/timg?searchbox_feed&quality=100&wh_rate=0&size=b576_324&ref=http%3A%2F%2Fwww.baidu.com&sec=1568739067&di=612dd27cae470b93b01a4b32ef72fbac&src=http%3A%2F%2Fpic.rmb.bdstatic.com%2Fe18c6ffa079441431f8988ca4c3ac106.jpeg"
          type="video/mp4"
          ref="videoSrouce"
        ></video>
        <div class="playBtn">
          <span class="iconfont iconshipin" @click="videoPlay" ref="playbtn"></span>
        </div>
        <!-- 暂停按钮 -->
        <div class="pause" @click="videoPause"></div>
      </div>
      <div class="header-video">
        <div class="head-img">
          <img :src="$axios.defaults.baseURL+postDetail.user.head_img" alt />
        </div>
        <span class="info">{{postDetail.user.nickname}}</span>
        <div
          class="header-Btn"
          @click="handleFollow"
          :class="{hasFollow:postDetail.has_follow}"
        >{{postDetail.has_follow?'已关注':'关注'}}</div>
      </div>
      <p class="title">{{postDetail.title}}</p>
    </div>
    <!-- 这里是公共部分 -->
    <div class="commonPart">
      <div class="btn-dianzan" @click="handleLike">
        <span class="iconfont icondianzan"></span>
        {{postDetail.like_length}}
      </div>
      <div class="btn-weixin">
        <span class="iconfont iconweixin"></span>
        微信
      </div>
    </div>
    <!-- 这里是精彩更贴部分 -->
    <div class="WonderPost">
      <h3>精彩跟帖</h3>
      <Comment
        @SendParentId="handleParentId"
        :Comdata="item"
        v-for="(item,index) in comments"
        :key="index"
      ></Comment>
    </div>
    <!-- 更多跟帖 -->
    <div v-if="comments.length">
      <router-link :to="'/morecomments?id=' + $route.query.id">
        <div class="morePost">更多跟帖</div>
      </router-link>
    </div>
    <div v-else class="NoComment">暂无跟帖，抢占沙发</div>
    <!-- 这里是公共页脚部分 -->
    <CommentInput
      ref="textarea"
      @upDateComment="loadComment"
      :parentId="parentId"
      :postId="$route.query.id"
      :nickname="nickname"
    ></CommentInput>
  </div>
</template>

<script>
// 引入评论组件
import Comment from "@/views/comments/comment.vue";
import CommentInput from "@/components/CommentInput.vue";
export default {
  // 注册组件
  components: {
    Comment,
    CommentInput
  },
  mounted() {
    this.loadPage();
    //页面一加载就调用获取用户评论的列表
    this.loadCommentPage();
    //重新加载评论页面
    this.loadComment();
  },
  data() {
    return {
      postDetail: {},
      showPlay: false,
      comments: [],
      parentId: "",
      nickname: ""
    };
  },
  methods: {
    loadPage() {
      //发送ajax请求获取数据
      this.$axios({
        url: "/post/" + this.$route.query.id,
        method: "get"
      }).then(res => {
        const { data } = res.data;
        this.postDetail = data;
        console.log(data);
      });
    },
    handleFollow() {
      if (this.postDetail.has_follow) {
        this.$axios({
          url: "/user_unfollow/" + this.postDetail.user.id,
          method: "get"
        }).then(res => {
          console.log("取消关注", res);
          const { message } = res.data;
          if (message == "未关注该用户") {
            this.postDetail.has_follow = false;
          }
        });
      } else {
        this.$axios({
          url: "/user_follows/" + this.postDetail.user.id,
          method: "get"
        }).then(res => {
          console.log("关注的测试", res);
          const { message } = res.data;
          if (message == "已关注") {
            this.postDetail.has_follow = true;
          }
        });
      }
    },
    //点赞
    handleLike() {
      this.$axios({
        url: "/post_like/" + this.postDetail.id,
        method: "get"
      }).then(res => {
        console.log("这是点赞测试", res);
        const { message } = res.data;
        if (message == "点赞成功") {
          this.postDetail.like_length++;
        } else if (message == "取消成功") {
          this.postDetail.like_length--;
        }
      });
    },
    // 点击按钮播放事件
    videoPlay(event) {
      //隐藏点击按钮
      event.target.style.display = "none";
      //调用video.play()函数
      const video = this.$refs.videoSrouce;
      video.play();
    },
    videoPause() {
      //显示按钮
      this.$refs.playbtn.style.display = "block";
      //暂停视频
      const video = this.$refs.videoSrouce;
      if (!this.showPlay) {
        video.pause();
      } else {
        this.$refs.playbtn.style.display = "none";
        video.play();
      }
      //将开关取反
      this.showPlay = !this.showPlay;
    },
    loadCommentPage() {
      this.$axios({
        url: "/post_comment/" + this.$route.query.id,
        method: "get"
      }).then(res => {
        console.log("评论列表", res);
        const { data } = res.data;
        //只显示三条数据
        if (data.length > 3) {
          data.length = 3;
        }
        this.comments = data;
      });
    },
    handleParentId(parentId, nickname) {
      //  让textarea显示
      this.$refs.textarea.showTextarea();
      console.log("这里是评论id", parentId);
      // 将parentid存到data中
      this.parentId = parentId;
      this.nickname = nickname;
    },
    loadComment() {
      //重新加载数据
      this.loadCommentPage();
    },
    goback() {
      this.$router.back();
    }
  }
};
</script>
<style lang="less" scoped>
.post-img {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 18px;
    border-bottom: 1px solid #eee;
    position: fixed;
    width: 100%;
    left: 0;
    top: 0;
    box-sizing: border-box;
    background-color: #f7f7f7;
    .header-left {
      display: flex;
      align-items: center;
      .iconjiantou2 {
        font-size: 4.167vw;
      }
      .iconnew {
        font-size: 15vw;
        padding-left: 4.167vw;
      }
    }
    .header-Btn {
      width: 17.222vw;
      height: 7.222vw;
      line-height: 7.222vw;
      font-size: 14px;
      text-align: center;
      background-color: #f00;
      color: #fff;
      border: 1px solid #fff;
      border-radius: 50px;
      &.hasFollow {
        background-color: #fff;
        color: #888;
        border: 1px solid #ccc;
      }
    }
  }
  .titlePost {
    padding: 7.222vw 5vw 7.778vw;
    margin-top: 62px;
    .title {
      font-size: 5vw;
      color: #333;
      font-weight: 700;
      margin-bottom: 12px;
    }
    .info {
      font-size: 14px;
      color: #868686;
      span {
        margin-right: 16px;
      }
    }
  }
}
// 内容部分
.mainContent {
  padding: 0 16px;
  font-size: 16px;
  /deep/ img {
    width: 100%;
  }
}

// 视频文章
.post-video {
  .header-video {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 18px;

    .head-img {
      img {
        width: 6.944vw;
        height: 6.944vw;
        border-radius: 50%;
        background-color: pink;
      }
    }
    .info {
      flex: 1;
      padding-left: 8px;
      font-size: 14px;
      color: #868686;
    }
    .header-Btn {
      width: 17.222vw;
      height: 7.222vw;
      line-height: 7.222vw;
      font-size: 14px;
      text-align: center;
      background-color: #f00;
      color: #fff;
      border: 1px solid #fff;
      border-radius: 50px;
      &.hasFollow {
        background-color: #fff;
        color: #888;
        border: 1px solid #ccc;
      }
    }
  }
  .title {
    font-size: 16px;
    color: #333;
    padding: 0 18px;
  }
  .mainVideo {
    position: relative;
    width: 100%;
    video {
      width: 100%;
    }
    .playBtn {
      .iconshipin {
        position: absolute;
        left: 42%;
        top: 30%;
        display: flex;
        justify-content: center;
        width: 16.111vw;
        height: 16.111vw;
        line-height: 16.111vw;
        text-align: center;
        font-size: 49px;
        color: #fff;
        background-color: rgba(0, 0, 0, 0.3);
        border-radius: 50%;
      }
    }
    .pause {
      position: absolute;
      left: 4%;
      bottom: 20%;
      width: 20px;
      height: 20px;
      background-color: transparent;
    }
  }
}
.commonPart {
  margin: 5.556vw 0;
  display: flex;
  justify-content: space-around;
  .btn-dianzan,
  .btn-weixin {
    height: 8.333vw;
    line-height: 30px;
    border: 1px solid #797979;
    padding: 0 5vw;
    border-radius: 4.167vw;
    text-align: center;
    font-size: 14px;
    span {
      font-size: 18px;
    }
    .iconweixin {
      color: #00c800;
    }
  }
}
.WonderPost {
  padding-top: 7.778vw;
  h3 {
    font-size: 5vw;
    color: #333;
    font-weight: normal;
    text-align: center;
  }
}
.morePost {
  width: 33.333vw;
  height: 8.333vw;
  line-height: 8.333vw;
  margin: 7.222vw auto 41.667vw;
  border: 1px solid #797979;
  text-align: center;
  border-radius: 4.167vw;
  font-size: 14px;
}
.NoComment {
  padding-top: 8.333vw;
  font-size: 3.889vw;
  color: #aeaeae;
  text-align: center;
  margin-bottom: 41.667vw;
}
</style>